﻿// Copyright (c) 2009 Kjetil Eraker Samuelsen
// This source is subject to the Microsoft Public License.
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
// All other rights reserved.

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using Ksamuelsen.Services.Vonage;


using OutlookLib = Ksamuelsen.Office.Outlook;

namespace Ksamuelsen.Office.Outlook.Addin.VDialer {
    [ComVisible(true)]
    public partial class VDialerConfigurationPropertyPage : OutlookLib.OutlookPropertyPage {
        private static Regex _isNumeric = new Regex(@"[^0-9]", RegexOptions.Compiled);

        public VDialerConfigurationPropertyPage() {
            InitializeComponent();
            AddValidator(_txtCountryCode, new OutlookLib.OutlookPropertyPage.ValidatorDelegate(ValidateNumericField), Properties.Resources.ERROR_NUMERIC_FIELD);
            AddValidator(_txtInternational, new OutlookLib.OutlookPropertyPage.ValidatorDelegate(ValidateNumericField), Properties.Resources.ERROR_NUMERIC_FIELD);
            AddValidator(_txtLongDistance, new OutlookLib.OutlookPropertyPage.ValidatorDelegate(ValidateNumericField), Properties.Resources.ERROR_NUMERIC_FIELD);
        }

        protected override void OnSaveSettings() {
            VDialerConfiguration config = VDialerConfiguration.Instance;

            config.UserName            = _txtUsername.Text;
            config.Password            = _txtPassword.Text;
            config.DisplayNumber       = _cmbPhoneNumber.Text;
            config.UseContactCenter    = _cbUseContactCenter.Checked;

            config.CountryCode         = _txtCountryCode.Text;
            config.InternationalPrefix = _txtInternational.Text;
            config.LongDistancePrefix  = _txtLongDistance.Text;
            config.IgnoreExtension     = _cbRemoveExtension.Checked;

            config.ShowDialDialog          = _cbShowDialog.Checked;
            config.ShowDialDialogWait      = _cbShowDialogWait.Checked;
            config.ShowDialDialogAutoClose = _cbShowDialogAutoClose.Checked;

            config.CreateJournal           = _cbCreateJournal.Checked;


            config.Save();

            base.OnSaveSettings();

        }

        protected override void OnLoadSettings() {
            base.OnLoadSettings();
            VDialerConfiguration config = VDialerConfiguration.Instance;

            _txtUsername.Text           = config.UserName;
            _txtPassword.Text           = config.Password;
            _cmbPhoneNumber.Text        = config.DisplayNumber;
            _cbUseContactCenter.Checked = config.UseContactCenter;

            _txtCountryCode.Text        = config.CountryCode;
            _txtInternational.Text      = config.InternationalPrefix;
            _txtLongDistance.Text       = config.LongDistancePrefix;
            _cbRemoveExtension.Checked  = config.IgnoreExtension;

            _cbShowDialog.Checked          = config.ShowDialDialog;
            _cbShowDialogWait.Checked      = config.ShowDialDialogWait;
            _cbShowDialogAutoClose.Checked = config.ShowDialDialogAutoClose;

            _cbCreateJournal.Checked = config.CreateJournal;

            CheckButtonState();
        }

        private bool ValidateNumericField(Control ctrl, string text) {
            if (String.IsNullOrEmpty(text))
                return true;

            return (!_isNumeric.IsMatch(text));
        }

        private void OnTextChanged(object sender, EventArgs e) {
            Dirty = true;
            CheckButtonState();

        }

        private void OnKeyPress(object sender, KeyPressEventArgs e) {
            /*if ((!Char.IsDigit(e.KeyChar)) && (!Char.IsControl(e.KeyChar))) {
                e.Handled = true;
                _errorProvider.SetError((Control)sender, "Only numeric values allowed.");
                Console.Beep();
            } else {
                _errorProvider.SetError((Control)sender, "");
            }*/
        }

        private void OnLookupNumbers(object sender, EventArgs e) {
            _btnLookup.Enabled = false;
            Cursor cursor = Cursor.Current;

            this.Cursor = Cursors.WaitCursor;
            try {
                string[] numbers = Click2Call.GetNumbers(_txtUsername.Text, _txtPassword.Text);
                _cmbPhoneNumber.Items.Clear();
                if ((numbers != null) && (numbers.Length > 0)) {
                    _cmbPhoneNumber.Items.AddRange(numbers);
                    if (String.IsNullOrEmpty(_cmbPhoneNumber.Text))
                        _cmbPhoneNumber.Text = numbers[0];
                }
            } catch (Exception) { }
            CheckButtonState();
            Cursor = cursor;
        }

        private void CheckButtonState() {
            _btnLookup.Enabled = !(String.IsNullOrEmpty(_txtUsername.Text) || String.IsNullOrEmpty(_txtPassword.Text));

            _btnJournalOptions.Enabled = _cbCreateJournal.Checked;

            _cbShowDialogAutoClose.Enabled = _cbShowDialog.Checked;
            _cbShowDialogWait.Enabled      = _cbShowDialog.Checked;

        }

        private void OnJournalOptions(object sender, EventArgs e) {
            VDialerJournalOptionsForm frm = new VDialerJournalOptionsForm();
            frm.ShowDialog(Form.ActiveForm);
        }

    }
}
